package com.yf.exam.modules.cert.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.core.utils.BeanMapper;
import com.yf.exam.modules.cert.dto.CertDTO;
import com.yf.exam.modules.cert.dto.request.CertReqDTO;
import com.yf.exam.modules.cert.dto.response.CertRespDTO;
import com.yf.exam.modules.cert.entity.Cert;
import com.yf.exam.modules.cert.mapper.CertMapper;
import com.yf.exam.modules.cert.service.CertService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 语言设置 服务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 13:23
*/
@Service
public class CertServiceImpl extends ServiceImpl<CertMapper, Cert> implements CertService {


    @Override
    public IPage<CertRespDTO> paging(PagingReqDTO<CertReqDTO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(), reqDTO.getParams());
     }

    @Override
    public String save(CertDTO reqDTO) {
        //复制参数
        Cert entity = new Cert();
        BeanMapper.copy(reqDTO, entity);
        this.saveOrUpdate(entity);
        return entity.getId();
    }

    @Override
    public List<CertDTO> listAll() {
        List<Cert> certList = this.list();
        List<CertDTO> dtoList = new ArrayList<>();
        for (Cert cert : certList) {
            CertDTO dto = new CertDTO();
            BeanMapper.copy(cert, dto);
            dtoList.add(dto);
        }
        return dtoList;
    }

    @Override
    public List<Object> getExamsByCertId(String certId) {
        // 通过证书ID查询关联的考试列表
        return baseMapper.getExamsByCertId(certId);
    }
}
